home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8492 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c,comp.lang.c++,comp.os.ms-windows.programmer.misc,comp.os.msdos.programmer,comp.programming,comp.windows.ms.programmer
  4. Subject: Re: Date Arithmetic
  5. Date: Fri, 16 Feb 96 22:12:21 GMT
  6. Organization: none
  7. Message-ID: <824508741snz@genesis.demon.co.uk>
  8. References: <4g19kp$640@tracy.protocom.com> <3124A458.7BF8@f25mail.amd.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <3124A458.7BF8@f25mail.amd.com>
  15.            carcher@f25mail.amd.com "Clark Archer" writes:
  16.  
  17. >Michael J. Karas wrote:
  18. >> 
  19. >> I am working on an algorithm for a laser marking machine that writes
  20. >> expiration delays on to food product boxes. The algorithm needs to
  21. >> be able to add NNN days to todays date in the fastest manner possible
  22. >> without using any floating point arithmetic. I could use help from anyone
  23. >> that has C code for doing this. It would be nice if the solution took the
  24. >> leap year problem in to account including the special case of the year
  25. >> 2000. Thanks in advance to anyone who could share their knowledge on this
  26. >> subject.
  27.  
  28. 2000 isn't a special case - it follows the normal Gregorian calendar rules.
  29.  
  30. >You could use the C runtime library function time() to get the current 
  31. >time and then add NNN * SECS_PER_DAY to it and then convert back to a 
  32. >struct tm like:
  33.  
  34. The C language does not define the representation of a time_t value, in
  35. particular it is not guaranteed to represent seconds.
  36.  
  37. >        
  38. >        #define SECS_PER_DAY  86400
  39. >
  40. >        time_t tmtNow;
  41. >        struct tm tmThen;
  42. >
  43. >        tmtNow = time(NULL);
  44. >        tmtNow += (nDaysToAdd * SECS_PER_DAY);
  45. >        tmThen = *localtime(tmtNow);
  46.  
  47. Note that localtime requires a pointer to a time_t value.
  48.  
  49. What you can do is given suitable tmtNow and nDaysToAdd
  50.  
  51.          tmThen = *localtime(&tmtNow);
  52.          tmThen.tm_mday += NDaysToAdd;
  53.          tmThen.tm_isdst = -1;   /* Optional - Allow change of DST status */
  54.          mktime(&tmThen);
  55.  
  56. mktime sets the members of tmThen correctly and you can also use its return
  57. value if you want a time_t. To be bullet-proof you should also test for
  58. failure.
  59.  
  60. The standard library time.h functions are defined to work according to
  61. the Gregorian calendar.
  62.  
  63. This may not be the 'fastest manner possible' but don't assume it is too
  64. slow before you try it.
  65.          
  66. -- 
  67. -----------------------------------------
  68. Lawrence Kirby | fred@genesis.demon.co.uk
  69. Wilts, England | 70734.126@compuserve.com
  70. -----------------------------------------
  71.